package Utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MathUtil {
	public static double compCos(double v1s[], double v2s[]) {
		double upper = 0.0;
		double lower1 = 0.0;
		double lower2 = 0.0;

		for (int i = 0; i < v1s.length; i++) {
			upper += v1s[i] * v2s[i];
			lower1 += v1s[i] * v1s[i];
			lower2 += v2s[i] * v2s[i];
		}
		double result = upper / (Math.sqrt(lower1) * Math.sqrt(lower2));
		return result;
	}

	public static double variance(ArrayList<Integer> counts) {
		double avg = average(counts);
		int total = counts.size();
		double sum = 0.0;
		for (int i = 0; i < counts.size(); i++)
			sum += (counts.get(i) - avg) * (counts.get(i) - avg);
		return MathUtil.precision((sum) / (total), Constants.PRECISION);

	}

	/**
	 * compute average number of a list
	 * 
	 * @param intervals
	 * @return
	 */
	public static double average(ArrayList<Integer> arrList) {
		double sum = 0;
		for (int i = 0; i < arrList.size(); i++) {
			sum += arrList.get(i);
		}
		return sum / arrList.size();
	}

	/**
	 * normalize a list of data to reduce the differences of dimension(量纲)
	 * 计算公式：y=(x-MinValue)/(MaxValue-MinValue)
	 * 
	 * @author 韩 闻文
	 * @param dataList
	 * @return
	 */
	public static ArrayList<Double> normalization(ArrayList<Integer> dataList) {
		ArrayList<Double> result = new ArrayList<Double>();
		double max = Double.MIN_VALUE;
		double min = Double.MAX_VALUE;
		for (int i = 0; i < dataList.size(); i++) {
			int data = dataList.get(i);
			if (data > max)
				max = data;
			if (data < min)
				min = data;
		}
		for (int i = 0; i < dataList.size(); i++) {
			double item = (dataList.get(i) - min) / (max - min);
			result.add(item);
		}

		return result;
	}

	/**
	 * normalize a list of data to reduce the differences of dimension(量纲)
	 * 计算公式：y=(x-MinValue)/(MaxValue-MinValue)
	 * 
	 * @author 韩 闻文
	 * @param dataList
	 * @return
	 */
	public static double normalization(ArrayList<Integer> dataList, Integer param) {
		double max = Double.MIN_VALUE;
		double min = Double.MAX_VALUE;
		for (int i = 0; i < dataList.size(); i++) {
			int data = dataList.get(i);
			if (data > max)
				max = data;
			if (data < min)
				min = data;
		}
		double item = (double) (param - min) / (max - min);
		return item;
	}

	public static double precision(double pDouble, int digitNumber) {
		BigDecimal bd = new BigDecimal(pDouble);
		BigDecimal bd1 = bd.setScale(digitNumber, BigDecimal.ROUND_HALF_UP);
		pDouble = bd1.doubleValue();
		return pDouble;
	}

	public static boolean isDigit(String str) {
		Pattern pattern = Pattern.compile("^\\d+$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	// 计算p（a|b）
	public static double conditionP(double pab, double pb) {
		double p = 0.0;
		p = pab / pb;
		return p;
	}

	// 计算pa，pb独立性
	public static double dependency(double pa, double pb, double pab) {
		double dependency = 0.0;
		dependency = pa * pb / pab;
		return dependency;
	}

	public static void main(String args[]) {
		double pa = 0.5;
		double pb = 0.4;
		double pab = 0.3;
		System.out.println(conditionP(pab, pb));
		System.out.println(conditionP(pab, pa));
		System.out.println(dependency(pa, pb, pab));

	}
}
